<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="完整编号" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入完整编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商圈楼栋" prop="options">
        <el-cascader
          v-model="queryParams.options"
          placeholder="请选择商圈楼栋"
          :options="options"
          @keyup.enter.native="handleQuery"
        ></el-cascader>
      </el-form-item>
      <el-form-item label="楼层" prop="floor">
        <el-input
          v-model="queryParams.floor"
          placeholder="请输入楼层"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="state">
        <el-select v-model="queryParams.state" placeholder="请选择状态" clearable>
          <el-option
            v-for="dict in dict.type.asset_house_state"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-setting"
          size="mini"
          @click="handleSettingRented"
        >默认收费项目(出租)
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-setting"
          size="mini"
          @click="handleSettingSale"
        >默认收费项目(出售)
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" border :data="houseList">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="房号" align="center" prop="code" />
      <el-table-column label="完整编号" align="center" prop="name" />
      <el-table-column label="商圈名称" align="center" prop="blockName" />
      <el-table-column label="楼栋名称" align="center" prop="buildingName" />
      <el-table-column label="楼层" align="center" prop="floor" />
      <el-table-column label="状态" align="center" prop="state">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.asset_house_state" :value="scope.row.state"/>
        </template>
      </el-table-column>
      <el-table-column label="状态结束时间" align="center" prop="stateEndTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.stateEndTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <!--      <el-table-column label="当前业主" align="center" prop="ownerId" />-->
      <el-table-column label="绑定的单位" align="center" prop="stateCompany" />
      <el-table-column label="占地面积" align="center" prop="buildingSquare" />
      <el-table-column label="使用面积" align="center" prop="useSquare" />
      <el-table-column label="当前租金" align="center" prop="rentFee" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" width="200px" fixed="right" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            :disabled="scope.row.state != 'empty'"
            @click="handleAddContract(scope.row)"
          >发起合同
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleViewContractList(scope.row)"
          >历史合同
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!--收费项的弹出层-->
    <fee-config v-if="showFeeConfig" ref="feeConfig"></fee-config>
    <!--发起-编辑合同-->
    <contract-form v-if="showContractForm" ref="contractForm" @after="getList"></contract-form>

    <!--查看历史合同的弹出层-->
    <contract-list v-if="showContractList" ref="contractList" @after="getList"></contract-list>
  </div>
</template>

<script>
import { listHouse} from "@/api/system/house";
import {getAllBuildingCascade} from "@/api/system/building";
import feeConfig from "@/views/pay/feeItem/fee-config";
import contractForm from "@/views/pay/contract/contract-form";
import contractList from "@/views/pay/contract/contract-list";

export default {
  name: "House",
  dicts: ['asset_house_state'],
  components: {
    feeConfig,
    contractForm,
    contractList
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商铺管理表格数据
      houseList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      showFeeConfig: false,
      //是否显示添加和修改合同的弹出层
      showContractForm:false,
      //是否显示当前商铺的历史合同信息
      showContractList:false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        blockId: null,
        buildingId: null,
        floor: null,
        state: null,
        options:[]
      },
      // 表单参数
      form: {
        options:[]
      },
      options:[]
    };
  },
  created() {
    this.getList();
    //查询商圈楼栋的级联数据
    getAllBuildingCascade().then(res=>{
      this.options=res.data;
    })
  },
  methods: {
    /** 查询商铺管理列表 */
    getList() {
      if(this.queryParams.options.length>0){
        this.queryParams.blockId=this.queryParams.options[0];
        this.queryParams.buildingId=this.queryParams.options[1];
      }
      this.loading = true;
      listHouse(this.queryParams).then(response => {
        this.houseList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        code: null,
        name: null,
        blockId: null,
        buildingId: null,
        floor: null,
        state: "empty"
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams.blockId=undefined;
      this.queryParams.buildingId=undefined;
      this.handleQuery();
    },
    /** 默认收费项目(出租) */
    handleSettingRented() {
      this.showFeeConfig = true;
      this.$nextTick(() => {
        this.$refs.feeConfig.init('rented', '设置默认收费项目(出租)');
      });
    },
    /** 默认收费项目(出售) */
    handleSettingSale() {
      this.showFeeConfig = true;
      this.$nextTick(() => {
        this.$refs.feeConfig.init('sale', '设置默认收费项目(出售) ');
      });
    },
    /** 发起合同 */
    handleAddContract(row) {
      this.showContractForm = true;
      this.$nextTick(() => {
        //id为houseId
        this.$refs.contractForm.init(row.id);
      });
    },
    /** 查看历史合同 */
    handleViewContractList(row) {
      this.showContractList = true;
      this.$nextTick(() => {
        //id为houseId
        this.$refs.contractList.init(row.id);
      });
    }
  }
};
</script>
